﻿using HSJM.Service.MWS.Core;
using HSJM.Service.MWS.Core.ShareMode;
using HSJM.Service.MWS.Infrastructure;
using HSJM.Service.MWS.Infrastructure.Dapper;
using HSJM.Service.MWS.Infrastructure.Repositories;
using StackExchange.Redis;
using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Threading.Tasks;
using System.Transactions;

namespace HSJM.Service.MWS.Application.Application.Base
{
    /// <summary>
    /// 
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class BaseApplication<T, TCondition> where T : IEntity
    {
        IBaseDomain<T, TCondition> _baseDomain { get; set; }
        public BaseApplication(IBaseDomain<T, TCondition> baseDomain)
        {
            _baseDomain=baseDomain;
        }

        /// <summary>
        /// 获取事物
        /// </summary>
        /// <returns></returns>
        public IDbTransaction GetTransaction()
        {
            return _baseDomain.GetTransaction();
        }

        #region 查询 
        /// <summary>
        /// 查询行数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public virtual Task<T> GetAsync(long id)
        {
            return _baseDomain.GetAsync(id);
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="sql"></param>
        /// <param name="param"></param>
        /// <param name="commandType"></param>
        /// <returns></returns>
        public virtual async Task<IEnumerable<T>> GetListAsync(object param = null, string sql = null, CommandType? commandType = null) {
            return await _baseDomain.GetListAsync(param, sql, commandType).ConfigureAwait(false);
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="condition"></param>
        /// <returns></returns>
        public virtual async Task<IEnumerable<T>> GetListAsync(TCondition condition) {
            return await _baseDomain.GetListAsync(condition).ConfigureAwait(false);
        }

        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="param"></param>
        /// <returns></returns>
        public virtual PagedResult<T> GetPageList(int pageIndex, int pageSize, string sql = null, object param = null)
        {
            var result = _baseDomain.GetPageList(pageIndex, pageSize, sql, param);
            return result;
        }
        #endregion

        #region 新增 
        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        [Transactional]
        public virtual async Task<HttpResponseResultModel<bool>> InsertAsync(T entity, IDbTransaction dbTransaction = null)
        {
            return await _baseDomain.InsertAsync(entity, dbTransaction).ConfigureAwait(false);
        }

        /// <summary>
        /// 批量新增
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        public virtual async Task<HttpResponseResultModel<bool>> InsertBatch(List<T> list)
        {
            var result=await _baseDomain.InsertBatch(list).ConfigureAwait(false);
            return result;
        }
        #endregion

        #region 修改 
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        [Transactional]
        public virtual async Task<HttpResponseResultModel<bool>> UpdateAsync(T entity, IDbTransaction dbTransaction = null)
        {
            return await _baseDomain.UpdateAsync(entity, dbTransaction).ConfigureAwait(false);
        }
        #endregion

        #region 删除 
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        [Transactional]
        public virtual async Task<HttpResponseResultModel<bool>> DeleteAsync(T entity, bool isDel = false, IDbTransaction dbTransaction = null)
        {
            return await _baseDomain.DeleteAsync(entity,isDel, dbTransaction).ConfigureAwait(false);
        }

        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        public virtual async Task<HttpResponseResultModel<bool>> DeleteBatch(List<T> list)
        {
            var result=await _baseDomain.DeleteBatch(list).ConfigureAwait(false);
            return result;

        }
        #endregion


    }
}
